﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace TGVR.Manager.UserControls.Template.Menu
{
    public partial class HeaderMenu : System.Web.UI.UserControl
    {
        #region data members
        public string SiteMapUrl { get; set; }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Server.MapPath(SiteMapUrl));
                XmlNodeList nodes = doc.ChildNodes;
                string strMenu = "<ul>";
                strMenu += PrintMenu(nodes[1].ChildNodes);
                strMenu += "</ul>";
                ltrMenu.Text = strMenu;
            }
        }

        private string PrintMenu(XmlNodeList nodes)
        {
            string result = "";
            foreach (XmlNode node in nodes)
            {
                if (node.Attributes["url"] == null || node.Attributes["title"] == null)
                    continue;
                if (node.ChildNodes.Count == 0)
                {
                    result += "<li><a href='" + node.Attributes["url"].Value.Replace("~", "")
                        + "'>";
                    if (node.Attributes["class"].Value == "menu-item-parent")
                    {
                        result += "<i class='fa fa-lg fa-fw " + node.Attributes["icon"].Value;
                        result += "'></i><span class='menu-item-parent'>";
                        result += node.Attributes["title"].Value + "</span></a></li>";
                    }
                    else
                    {
                        result += node.Attributes["title"].Value + "</a></li>";
                    }
                }
                else
                {
                    result += "<li><a href='" + node.Attributes["url"].Value.Replace("~", "")
                        + "'>";
                    if (node.Attributes["class"].Value == "menu-item-parent")
                    {
                        result += "<i class='fa fa-lg fa-fw " + node.Attributes["icon"].Value;
                        result += "'></i><span class='menu-item-parent'>";
                        result += node.Attributes["title"].Value + "</span></a>";
                    }
                    else
                    {
                        result += node.Attributes["title"].Value + "</a>";
                    }
                    result += "<ul>";
                    result += PrintMenu(node.ChildNodes);
                    result += "</ul>";
                    result += "</li>";
                }
            }
            return result;
        }
    }
}